/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.taggedDocs.client;



import com.gadglet.client.gwt.GadgetNativeUtils;
import com.gadglet.client.gwt.GadgetUtils;
import com.gadglet.client.gwt.google.DocsFolderItem;
import com.gadglet.client.gwt.google.GoogleRequest;
import com.gadglet.client.gwt.ui.HomeViewMainPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


public class DocsList  extends GoogleRequest{
	protected final PreferencesFeature prefs = PreferencesProvider.get();
	final VerticalPanel holder = new VerticalPanel();
	
	final TextBox tagWordsTextBox = new TextBox();;
	final Button changeButton = new Button(prefs.getMsg("gadgetLabelChange"));;
	final Button saveButton= new Button(prefs.getMsg("gadgetLabelSave"));;
	final HorizontalPanel changeWordsPanel = new HorizontalPanel();;
	final VerticalPanel documentsListPanel  = new VerticalPanel(); ;
	
	public DocsList(){
		super();
	}
	public DocsList(HomeViewMainPanel mainPanel){
		super();

		if(prefs.getString("selectedWords")!=null && !prefs.getString("selectedWords").isEmpty())
			tagWordsTextBox.setText(prefs.getString("selectedWords"));
		else
			tagWordsTextBox.setText(prefs.getMsg("gadgetMsgEnterKeyWords"));
		mainPanel.add(holder);
		changeWordsPanel.add(tagWordsTextBox);
		changeWordsPanel.add(saveButton);
		changeWordsPanel.setSpacing(2);
		changeWordsPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);;
	    holder.add(changeButton);
	    holder.add(changeWordsPanel);
	    
	  
	    tagWordsTextBox.addClickHandler(new ClickHandler() {
	        @Override
			public void onClick(final ClickEvent event) {
	           ((TextBox)event.getSource()).setText("");
	        }
	    });
	    
	    changeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				changeWordsPanel.setVisible(true);
				changeButton.setVisible(false);
			}
		});
	    
	    
	    saveButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String text = tagWordsTextBox.getText();
				// check if empty
				if(text!= null && !text.isEmpty())
				{
					// save
					GadgetNativeUtils.setTitle(text);
					prefs.set("selectedWords", text);
					
					changeButton.setVisible(true);
					changeWordsPanel.setVisible(false);
					getDocs();	
				}
				else{
					//
				}
			
			}
		});
	   
	    holder.setSpacing(10);
	    holder.setVisible(true);
	}

	void init(){
		
		if(prefs.getString("selectedWords")!=null && !prefs.getString("selectedWords").isEmpty())
		{
			GadgetNativeUtils.setTitle(prefs.getString("selectedWords"));
			changeWordsPanel.setVisible(false);
			changeButton.setVisible(true);
			getDocs();
		}
		else
		{
			changeButton.setVisible(false);
			changeWordsPanel.setVisible(true);
			
		}
	
	}
	
	void getDocs()
	{
		itemsList.clear();
		// remove old results
		
		String selected = prefs.getString("selectedWords");
		if(prefs.getString("selectedWords")==null || prefs.getString("selectedWords").isEmpty())
			selected = "a";
		selected = selected.replace(" ", "+");
		
		String feedUrl =  "https://docs.google.com/feeds/documents/private/full";
		feedUrl = feedUrl  +"?alt=json"+"&q="+ selected;
		
		feedUrl = URL.encode(feedUrl);
		setFeedUrl(feedUrl);
		setHeaders("GData-Version","2.0");
		setAuthorizationMsg("gadgetMsgPleaseApproveDocs");
		callFeed();
		
		
	}
	@Override
	protected void  processResults(){
		
		documentsListPanel.clear();
			
		Label emptyFolder = new Label(prefs.getMsg("gadgetMsgNoResults"));
		emptyFolder.setStyleName("errorMsg");
		DocsFolderItem dh = null;
	
		HorizontalPanel  docRecord = null; 
		
		Anchor doc = null;
		Image docIcon = null;
		
		
		if(itemsList.size() >0){
			for (int y =0; y< itemsList.size(); y++){
			  docRecord = new HorizontalPanel();
			  docRecord.setSpacing(3);
			  dh = new DocsFolderItem(itemsList.get(y));
			  
			  docIcon = new Image(GadgetUtils.getGadgetIconURL(dh.getDocType(), null, false));
			  docRecord.add(docIcon);
			  
			  doc = new Anchor();
			  doc.setHTML(dh.getTitle());
			  doc.setHref(dh.getDocURL());
	    	  doc.setTarget("_blank");
	    	
	    	  docRecord.add(doc);
	    	 
	    	  documentsListPanel.add(docRecord);
	      }
		}
		else
			documentsListPanel.add(emptyFolder);
	   	      
	  	holder.add(documentsListPanel);
	}
}
